﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace thailandProject.ViewModel
{
    public class DescPair
    {
        public DescPair(string desc, string val)
        {
            description = desc;
            defaultValue = val;
        }
            
        public string description;
        public string defaultValue;
    }

    public class DictData
    {
        public string value;
        public int count;
        public bool repeat;
        public int match;// -1 = None, 0 = Complete, 1 = Complete-Partial, 2 = Partial
        
        public DictData(string value, int count = -1, bool repeat = false, int match = -1)
        {
            this.value = value;
            this.count = count;
            this.repeat = repeat;
            this.match = match;
        }
    }
    
    public class EmailTemplateModel
    {
        /// <summary>
        /// The type of template to show on the view
        /// </summary>
        public string templateType;

        /// <summary>
        /// The raw, unformatted XML text of the template
        /// </summary>
        public string templateText;

        public Dictionary<string, DictData> sentenceList;

        public Dictionary<string, DescPair> tagCloud;

        /// <summary>
        /// Creates a template model object. This will parse out the XML text into something that the View and controller
        /// can use
        /// </summary>
        /// <param name="templateType">The type of template to use on the view</param>
        /// <param name="templateText">The raw XML text</param>
        public EmailTemplateModel(string templateType, string templateText)
        {
            this.templateType = templateType;
            this.templateText = templateText;
            sentenceList = new Dictionary<string, DictData>();
            tagCloud = EmailTemplateModel.getTagClound();

            processXML();
        }

        private void processXML()
        {
            string xml = String.Copy(templateText); //Don't muck up the original string

            //Look for the opening <Template> tag
            if (xml.IndexOf("<Template>") == 0)
            {
                xml = xml.Substring(xml.IndexOf(">") + 1); //Remove the <Template>

                string current = ""; 
                while(xml.IndexOf("</Tag>") != -1) //Stop when no more tags exist
                {
                    xml = xml.Substring(xml.IndexOf("<Tag>") + 5); //Remove the <Tag>

                    current = xml.Substring(0, xml.IndexOf("</Tag>")); //Get the data inbetween the two tags
                    xml = xml.Substring(xml.IndexOf("</Tag>") + 6); //Remove the tag that was just read

                    string data;
                    string key = "", value = "";
                    int count = -1, match = -1;
                    bool repeat = false;
                    while (current.Length != 0)
                    {
                        data = current.Substring(current.IndexOf("<") + 1, current.IndexOf(">") - 1);
                        current = current.Substring(current.IndexOf(">") + 1); //Remove the tag's start

                        if(data == "Name")
                            key = current.Substring(0, current.IndexOf("</Name>"));
                        else if(data == "Value")
                            value = current.Substring(0, current.IndexOf("</Value>"));
                        else if (data == "Count")
                            count = Convert.ToInt32(current.Substring(0, current.IndexOf("</Count>")));
                        else if (data == "Repeat")
                            repeat = Convert.ToBoolean(current.Substring(0, current.IndexOf("</Repeat>")));
                        else if (data == "Type")
                        {
                            switch(current.Substring(0, current.IndexOf("</Type>")))
                            {
                                case "Complete": match = 0; break;
                                case "Complete-Partial": match = 1; break;
                                case "Partial": match = 2; break;
                                default: match = -1; break;
                            }
                        }

                        current = current.Substring(current.IndexOf("</") + data.Length + 3); //Remove the currently read tag
                    }//End to while
                    sentenceList.Add(key, new DictData(value, count, repeat, match));
                }//End to while
            }//End to if
            else
            {
                throw new Exception("XML does not contain opening <Template> tag at position 0");
            }
        }//End to processXML

        public static string defaultOnPropInquiry()
        {
            string rt = "";
            rt += "<Template>" +
                    "<Tag>" +
                        "<Name>Introduction</Name>" +
                        "<Value>Dear [clientName],\n\nThank you for your interest in Chiang Mai Apartment Rentals.</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>No Properties Available</Name>" +
                        "<Value>Unfortunately, I do not have any apartments available for the dates you wished to stay. \n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Multiple Complete Matches Available</Name>" +
                        "<Value>I recommend you stay in one of the apartments listed below during your stay from [resStartDate] to [resEndDate]:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>One Complete Match Available</Name>" +
                        "<Value>I recommend you stay in the following apartment from [resStartDate] to [resEndDate]:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Complete Match Property Information Model</Name>" +
                        "<Value>   Apartment [propName] - [propHyperlink]\n      Price - [quotedPrice]\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Complete and Partial Matches Available</Name>" +
                        "<Value>Alternatively, I have the following properties also available. While you won't be able to stay in one apartment, you will be able to see more of the city during your stay:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Complete and One Partial Match Available</Name>" +
                        "<Value>Alternatively, I have this property available:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Complete-Partial Property Information Model</Name>" +
                        "<Value>   Apartment [propName] - [propHyperlink]\n      Available for: [startDate] to [endDate]\n      Price - [quotedPrice]\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Multiple Partial Matches Available</Name>" +
                        "<Value> While I do not have any apartments available for your entire stay, I do have some properties that are partly available. The advantage to this is that you will be able to see more of the city during your stay:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>One Partial Match Available</Name>" +
                        "<Value> While I do not have any apartments available for your entire stay, I do have a property that is partly available. While it does not work for your entire stay, I will give you the option:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Partial Match Property Information Model</Name>" +
                        "<Value>   Apartment [propName] - [propHyperlink]\n      Available for: [startDate] to [endDate]\n      Price - [quotedPrice]\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Summary Matches Found</Name>" +
                        "<Value>Please be aware that apartments go quickly. If you are serious about staying here, please make your deposit as soon as you can.</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Summary No Matches Found</Name>" +
                        "<Value>I apologize for being unable to accomodate you at this time. Please remember us for a future stay and we will try our best to have something available for you.</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Signature</Name>" +
                        "<Value>\n\n-Richard Katze\n\nI accept PayPal ([paypal])\n\n[signature]</Value>" +
                    "</Tag>" +
                "</Template>";

            return rt;
        }

        public static string defaultOnReservationEdit()
        {
            string rt = "";
            rt += "<Template>" +
                    "<Tag>" +
                        "<Name>Introduction</Name>" +
                        "<Value>Dear [clientName],\n\nI have made the follow changes to your reservation:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Reservation Date Changed</Name>" +
                        "<Value>Your original reservation was from [resOldStartDate] to [resOldEndDate]. The new reservation is from [resStartDate] to [resEndDate].\n\n" +
                        "</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Reservation Quoted Price Changed</Name>" +
                        "<Value>Your original quoted price for your reservation was [resOldQuotedPrice]. The new quoted price for your reservation is [resQuotedPrice].\n\n" +
                        "</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Reservation Confirmation Changed to Confirmed</Name>" +
                        "<Value>Your reservation between [resStartDate] and [resEndDate] for [propName] has been confirmed and is being held for you.\n\n" +
                        "</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Reservation Confirmation Changed to Unconfirmed</Name>" +
                        "<Value>Your reservation between [resStartDate] and [resEndDate] for [propName] is no longer being held and will require a deposit if you wish to keep your reservation.\n\n" +
                        "</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Summary</Name>" +
                        "<Value>Please let me know if these changes are acceptable.\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Signature</Name>" +
                        "<Value>-Richard Katze\n\n[signature]</Value>" +
                    "</Tag>" +
                "</Template>";

            return rt;
        }

        public static string defaultOnDepositMade()
        {
            string rt = "";
            rt += "<Template>" +
                    "<Tag>" +
                        "<Name>Introduction</Name>" +
                        "<Value>Dear [clientName],\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Body</Name>" +
                        "<Value>I have received your payment of [paymentAmount]. " +
                                "Due to third party fees, please be aware that the amount I received may not be the amount that you sent to me.\n\n" +
                                "The amount you currently owe is: [remainingAmount].\n\n" +
                                "The remaining balance will be paid when you arrive on [resStartDate]." +
                                "Thank you for staying with us. I look forward to meeting you.\n\n" +
                        "</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Signature</Name>" +
                        "<Value>-Richard Katze\n\n[signature]</Value>" +
                    "</Tag>" +
                "</Template>";

            return rt;
        }

        public static string defaultOnRefundMade()
        {
            string rt = "";
            rt += "<Template>" +
                    "<Tag>" +
                        "<Name>Introduction</Name>" +
                        "<Value>Dear [clientName],\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Body</Name>" +
                        "<Value>I have refunded your payment of [refundAmount]. " +
                        "This amount only covers the amount I have received, so it may be more depending on if third party fees were refunded as well. " +
                        "I apologize for any inconvenience this has caused you.\n\n" +
                        "</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Signature</Name>" +
                        "<Value>-Richard Katze\n\n[signature]</Value>" +
                    "</Tag>" +
                "</Template>";

            return rt;
        }

        public static string defaultOnReservationDrop()
        {
            string rt = "";
            rt += "<Template>" +
                    "<Tag>" +
                        "<Name>Introduction</Name>" +
                        "<Value>Dear [clientName],\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Single Property Cancelled</Name>" +
                        "<Value>Your reservation for [resStartDate] to [resEndDate] on property [propName] ([propHyperlink]) has been cancelled due to another client making a deposit on this property with a similar time range.\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Multiple Properties Cancelled</Name>" +
                        "<Value>Your reservation for the follow properties have been cancelled due to other clients making a deposit for these properties for a similar time:\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Property Information Model</Name>" +
                        "<Value>   Apartment [propName] - [propHyperlink]\n      Stay times: [resStartDate] to [resEndDate]\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Summary</Name>" +
                        "<Value>I apologize for any inconvenience this has caused. If you would like to see if I have another property available, please reply and I will do my best to accommodate you. Thank you for your interest in Chiang Mail Apartment Rentals.\n\n</Value>" +
                    "</Tag>" +
                    "<Tag>" +
                        "<Name>Signature</Name>" +
                        "<Value>-Richard Katze\n\n[signature]</Value>" +
                    "</Tag>" +
                "</Template>";
            return rt;
        }

        public static string defaultOnError()
        {
            string rt = "";
            rt += "<Template>" +
                    "<Tag>" +
                        "<Name>Sentence 1</Name>" +
                        "<Value>Uh-oh... an error occurred. Try pressing your browser's back button...</Value>" +
                    "</Tag>" +
                "</Template>";

            return rt;
        }

        public static Dictionary<string, DescPair> getTagClound()
        {
            Dictionary<string, DescPair> rt = new Dictionary<string, DescPair>();

            rt.Add("[clientName]", new DescPair("The client's name.", "John Doe"));
            rt.Add("[paymentAmount]", new DescPair("The amount the client just paid.", "฿ 38354"));
            rt.Add("[refundAmount]", new DescPair("The amount that was refunded to the client.", "฿ 38354"));
            rt.Add("[remainingAmount]", new DescPair("The amount the client still needs to pay.", "฿ 41646"));
            rt.Add("[resStartDate]", new DescPair("The date that the client's reservation will start.", "Jan 31st, 2013"));
            rt.Add("[resEndDate]", new DescPair("The date that the client's reservation will end.", "Feb 22st, 2013"));
            rt.Add("[resOldStartDate]", new DescPair("The date that the client's reservation used to start.", "Jan 10th, 2013"));
            rt.Add("[resOldEndDate]", new DescPair("The date that the client's reservation used to end.", "Feb 5th, 2013"));
            rt.Add("[resQuotedPrice]", new DescPair("The quoted price for a reservation.", "฿ 80000"));
            rt.Add("[quotedPrice]", new DescPair("The quoted price for an inquiry.", "฿ 80000"));
            rt.Add("[resOldQuotedPrice]", new DescPair("The incorrect quoted price for a reservation.", "฿ 40000"));
            rt.Add("[propName]", new DescPair("The name of the property.", "C614"));
            rt.Add("[propHyperlink]", new DescPair("The hyperlink of the property.", "<a href='http://www.chiangmaiapartmentrental.com/p102.html'>http://www.chiangmaiapartmentrental.com/p102.html</a>"));
            rt.Add("[signature]", new DescPair("Contains images, links, and text for your signature.", "<img src='../../images/rated-excellent.png' alt='Rated excellent' /><br /><br />" +
                                               "<img src='../../images/award-2012-2.png' alt='Winner 2012' /><br /><br />" +
                                               "Richard Katze<br />66(0)861162233<br /><a href='http://www.chiangmaiapartmentrental.com/'>www.chiangmaiapartmentrental.com</a><br />" +
                                               "<a href='mailto:chiangmairentals@gmail.com'>chiangmairentals@gmail.com</a><br /><br />" +
                                               "MY TOUR GUIDE<br /><a href='http://www.chiangmaitourguide.com/'>www.chiangmaitourguide.com</a><br /><br />" +
                                               "<img src='../../images/logo5.jpg' alt='Chiang Mai Apartment Rentals' />"
                                               ));
            rt.Add("[paypal]", new DescPair("The link to your PayPal", "<a href='https://www.paypal.com/webapps/mpp/make-online-payments'>chiangmairentals@gmail.com</a>"));

            return rt;
        }
    }//End to class
}